<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>千园千变，万班万课</title>
    <meta name="description" content="这是一个 index 页面">
    <meta name="keywords" content="index">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="icon" type="image/png" href="assets/i/favicon.png">
    <link rel="apple-touch-icon-precomposed" href="assets/i/app-icon72x72@2x.png">
    <meta name="apple-mobile-web-app-title" content="Amaze UI"/>
    <link rel="stylesheet" href="assets/css/amazeui.min.css"/>
    <link rel="stylesheet" href="assets/css/admin.css">
    <link rel="stylesheet" href="assets/css/app.css">
    <link rel="stylesheet" href="assets/css/viewer.min.css">
</head>
<body data-type="generalComponents">
<div class="tpl-portlet-components">
    <div class="portlet-title">
        <ol class="am-breadcrumb">
            <li><a href="#" class="am-icon-home">首页</a></li>
            <li><a href="#">Amaze UI CSS</a></li>
            <li class="am-active">文字列表</li>
        </ol>
        <button class="refresh_btn"><i class="am-icon-refresh"></i></button>
    </div>
    <div class="tpl-block">
        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-9">
                <div class="am-btn-toolbar check_permission[1]">
                    <div class="am-btn-group am-btn-group-xs">
                        <button type="button" class="am-btn am-btn-default am-btn-success btn_add"><span
                                class="am-icon-plus"></span> 添加学校
                        </button>
                    </div>
                </div>
            </div>
            <div class="am-u-sm-12 am-u-md-3">
                <div class="am-input-group am-input-group-sm">
                    <input type="text" id="search" class="am-form-field" placeholder="请输入关键字">
                    <span class="am-input-group-btn">
                        <button class="am-btn  am-btn-default am-btn-success tpl-am-btn-success am-icon-search"
                                type="button" id="searchBtn"></button>
                    </span>
                </div>
            </div>
        </div>
        <div class="am-g">
            <div class="am-u-sm-12">
                <table class="am-table am-table-striped am-table-hover table-main" id="table">
                    <thead>
                    <tr>
                        <th class="table-id" width="120">ID</th>
                        <th class="table-title">学校名称</th>
                        <th class="table-type">登录账号</th>
                        <th class="table-type">学校地址</th>
                        <th class="table-author ">联系人/电话</th>
                        <th class="table-author ">负责老师</th>
                        <th class="table-author ">合同照片</th>
                        <th class="table-set" width="240">操作</th>
                    </tr>
                    </thead>
                    <tbody></tbody>
                </table>
                <div class="am-cf">
                    <div class="am-fr">
                        <div class="am-pagination tpl-pagination" id="pagination"></div>
                    </div>
                </div>
                <hr>
            </div>
        </div>
    </div>
</div>
<!--修改密码-->
<div class="am-g tpl-amazeui-form model_panel" id="editPasswordPanel" style="display: none">
    <div class="am-u-sm-12">
        <form class="am-form am-form-horizontal">
            <div class="am-form-group">
                <label class="am-u-sm-3 am-form-label">学校名称</label>
                <div class="am-u-sm-9 line">
                    <b id="school_name" class="am-form-label"></b>
                </div>
            </div>
            <div class="am-form-group">
                <label class="am-u-sm-3 am-form-label">账号</label>
                <div class="am-u-sm-9 line">
                    <input type="number" id="username" name="username" style="width: 300px" placeholder="输入登录账号">
                </div>
            </div>
            <div class="am-form-group">
                <label for="one_pwd" class="am-u-sm-3 am-form-label">密码</label>
                <div class="am-u-sm-9">
                    <input type="password" id="one_pwd" name="one_pwd" style="width: 300px" placeholder="输入密码">
                </div>
            </div>
            <div class="am-form-group">
                <label for="two_pwd" class="am-u-sm-3 am-form-label">确认密码</label>
                <div class="am-u-sm-9">
                    <input type="password" id="two_pwd" name="two_pwd" style="width: 300px"
                           placeholder="输入确认密码">
                </div>
            </div>
        </form>
    </div>
</div>
<!--选择购买课程-->
<div class="am-g tpl-amazeui-form model_panel" id="courseBuy" style="display: none">
    <div class="am-u-sm-12">
        <form class="am-form am-form-horizontal">
            <div class="am-form-group">
                <label class="am-u-sm-3 am-form-label">学校名称</label>
                <div class="am-u-sm-9 line">
                    <b id="buy_school_name" class="am-form-label"></b>
                </div>
            </div>
            <div class="am-form-group">
                <label class="am-u-sm-3 am-form-label">班级数量</label>
                <div class="am-u-sm-9 line">
                    <select class="am-form-field" id="class_num" name="class_num" style="width: 150px"></select>
                    <span class="tips"></span>
                </div>
            </div>
            <div class="am-form-group">
                <label class="am-u-sm-3 am-form-label">每周课程</label>
                <div class="am-u-sm-9">
                    <label class="am-radio-inline">
                        <input type="radio" value="2" name="course" checked> 每周2节课
                    </label>
                   <!-- <label class="am-radio-inline">
                        <input type="radio" value="4" name="course" > 每周4节课
                    </label>-->
                </div>
            </div>
            <div class="buy_tips">
                <p class="info">定制内容：<span id="selectClassNum">1</span>个班+每周 <span id="selectCourseNum">2</span>节课；</p>
                <div class="price">金额：<font>¥</font><span id="priceNum">0</span></div>
            </div>
        </form>
    </div>
</div>
<div class="pay_model">
    <div class="wechat-modal">
        <button class="close_btn">&times;</button>
        <h1 class="price">微信支付￥<span id="ewmPrice">0</span></h1>
        <div class="qrcode js-qrcode-img" id="ewm"></div>
        <p>使用微信扫描二维码进行支付</p>
    </div>
</div>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/amazeui.min.js"></script>
<script src="assets/js/layer.js"></script>
<script src="assets/js/jquery.cookie.js"></script>
<script src="assets/js/md5.min.js"></script>
<script src="assets/js/jquery.pagination.js"></script>
<script src="assets/js/jquery.qrcode.min.js"></script>
<script src="assets/js/jquery.serializejson.min.js"></script>
<script src="assets/js/request.js"></script>
<script src="assets/js/verification.js"></script>
<script src="assets/js/viewer.min.js"></script>
<script src="assets/js/app.js"></script>

</body>
<script>
var page = '1'
var size = '10'
var search = ''
var type = ''
var classV = ''
var val = ''
var total = 0
var listData = null
function getDataList (callback) {
  AjaxRequest({
    url: '/admin/school_list/',
    data: {page, size, search},
    succFun: function (res) {
      var data = res.data
      total = data.count
      var html = ''
      listData = res.data.list
      listData.forEach(function (d, i) {
        html += ` <tr>
                    <td>${d.school_id}</td>
                    <td><a href="#" class="btn_edit"   data-id="${d.school_id}">${d.school_name}</a></td>
                    <td>${d.username || '--'}</td>
                    <td>${d.city_name}</td>
                    <td>${d.contacts}/${d.phone}</td>
                    <td>${d.teacher_name || '--'}</td>
                    <td class="card_content"> ${d.type == 2 ? '<p><img src="' + d.contract + '" alt=""></p>' : '--'}</td>
                    <td>
                        <div class="am-btn-toolbar">
                            <div class="am-btn-group am-btn-group-xs">
                                <button class="am-btn am-btn-success am-btn-xs btn_edit"
                                data-id="${d.school_id}"
                                ><span class="am-icon-pencil-square-o"></span> 编辑</button>
                                 <button class="am-btn am-btn-warning am-btn-xs btn_buy_course"
                                data-id="${d.school_id}"
                                data-name="${d.school_name}"
                                data-username="${d.username}"
                               ${d.is_order!=0?'disabled':''}
                                >${d.is_order==0?'购买课程':'已购买'}</button>
                                    <button class="am-btn am-btn-secondary am-btn-xs btn_edit_password"
                                data-id="${d.school_id}"
                                data-name="${d.school_name}"
                                data-username="${d.username}"
                                >修改密码</button>
                            </div>
                        </div>
                    </td>
                </tr>`
      })
      $('#table tbody').html(html)
      callback && callback()
    }
  })
}
function init () {
  getDataList(function () {
    $('#pagination').pagination({
      pageCount: Math.ceil(total / size),
      jump: true,
      coping: true,
      keepShowPN: true,
      prevContent: '«',
      nextContent: '»',
      jumpBtnCls: 'am-btn am-btn-primary',
      activeCls: 'am-active',
      callback: function (api) {
        page = api.getCurrent().toString()
        getDataList()
      }
    });
  })
}
init()
var ids = []
$('table').on('change', 'tbody [type="checkbox"]', function () {
  ids = []
  var $tableTbody = $('table tbody [type="checkbox"]')
  $tableTbody.each(function (i, d) {
    if ($(d).prop('checked')) {
      ids.push($(this).val())
    }
  })
  if ($tableTbody.length === ids.length) {
    $('table thead [type="checkbox"]').prop('checked', true)
  } else {
    $('table thead [type="checkbox"]').prop('checked', false)
  }
})
$('table thead [type="checkbox"]').change(function () {
  ids = []
  var isChecked = $(this).prop('checked')
  $('table tbody [type="checkbox"]').each(function (i, d) {
    if (isChecked) {
      $(d).prop('checked', true)
      ids.push($(this).val())
    } else {
      $(d).prop('checked', false)
    }
  })
})
//修改或新增
$('body').on('click', '.btn_edit,.btn_add', function (e) {
  e.preventDefault()
  let id = $(this).attr('data-id')
  location.href = 'school_update_or_add.html?id=' + id
})
//搜索
$('#searchBtn').click(function (e) {
  e.preventDefault()
  search = $('#search').val().toString()
  page = '1'
  init()
})
//修改密码
$('body').on('click', '.btn_edit_password', function (e) {
  e.preventDefault()
  let school_id = $(this).attr('data-id')
  let school_name = $(this).attr('data-name')
  let userName = $(this).attr('data-username')
  $('#school_name').html(school_name)
  $('#username').val(userName)
  layer.open({
    type: 1,
    title: '修改账号密码',
    area: ['500px', '380px'],
    shadeClose: true, //点击遮罩关闭
    btn: ['确定', '取消'],
    content: $('#editPasswordPanel'),
    yes: function () {
      if (Verification()) {
        var formData = $('#editPasswordPanel form').serializeJSON()
        formData.school_id = school_id
        AjaxRequest({
          url: '/admin/school_set/',
          data: formData,
          succFun: function (res) {
            layer.msg(res.message, {icon: 1, time: 1500}, function () {
              layer.closeAll()
              getDataList()
            })
          }
        })
      }

    }
  });
})
//查看图
$('table').on('click', '.card_content img', function () {
  $(this).viewer();
})
// 购买课程
var timer=null
var order_number=null
$('table').on('click', '.btn_buy_course', function (e) {
  e.preventDefault()
  let school_id = $(this).attr('data-id')
  let school_name = $(this).attr('data-name')
  let userName = $(this).attr('data-username')
  $('#buy_school_name').html(school_name)
  $('#username').val(userName)
  layer.open({
    type: 1,
    title: '购买课程',
    area: ['500px', '380px'],
    shadeClose: true, //点击遮罩关闭
    btn: ['确定', '取消'],
    content: $('#courseBuy'),
    yes: function () {
      var formData = $('#courseBuy form').serializeJSON()
      if(formData.class_num == '0'){
        layer.msg('请选择上课班级数量',{icon:0})
        return false
      }
      formData.school_id = school_id
      AjaxRequest({
        url: '/wxpay/jsapi_pay_pc/',
        data: formData,
        succFun: function (res) {
          order_number=res.data.order_number
          var payUrl=res.data.wx_order
          $('#ewm').qrcode(payUrl);
          $('.pay_model').fadeIn()
          timer=setInterval(checkorderinfo, 3000);
        }
      })
    }
  });
})
// PC支付成功主动调用接口
function checkorderinfo () {
  AjaxRequest({
    url: '/wxpay/pay_pc_success/',
    data: {order_number},
    succFun: function (res) {
      var state=res.data.state
      console.log(state)
      if(state==1){
        layer.closeAll()
        $('.pay_model').hide()
        clearInterval(timer)
        $('#ewm').html('')
        getDataList()
      }
    }
  })
}
//设置课程数量
function setClassNum () {
  var html = '<option value="0">请选择班级数量</option>'
  for (var i = 3; i <= 30; i++) {
    html += `<option value="${i}">${i}个班</option>`
  }
  $('#class_num').html(html)
}
setClassNum()
// 设置价格
function setPrice (class_num, course) {
  var price = class_num * course * 50
  $('#priceNum,#ewmPrice').html(price)
  $('#selectClassNum').html(class_num)
  $('#selectCourseNum').html(course)
}
$('#class_num').change(function () {
  var class_num = $(this).val()
  var course = $('[name="course" ]:checked').val()
  setPrice(class_num, course)
})
$('[name="course" ]').change(function () {
  var course = $(this).val()
  var class_num = $('#class_num').val()
  setPrice(class_num, course)
})
$('.close_btn').click(function () {
  $('.pay_model').hide()
  clearInterval(timer)
  $('#ewm').html('')
})
</script>
</html>